<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">十蜂健康出入库系统</h3>
      </div>
      <div class="loginType">
        <!-- 三元运算判断选中样式 -->
        <div :class="[currentLogin ? 'passwordLogin' : 'loginCheck']" @click="passwordLogin">账号密码登录</div>
        <div :class="[currentLogin == false ? 'phoneLogin' : 'loginCheck']" @click="phoneLogin">手机号码登录</div>
      </div>
      <div v-show="currentLogin">
        <!-- 账号密码登录 -->
        <!-- 用户名输入框 -->
        <el-form-item prop="username" style="margin-top:10px">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入账号"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <!-- 密码显示隐藏 -->
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <!-- 忘记密码按钮 -->
        <div class="forget" @click="forget">忘记密码?</div>
        <!-- 登录提交信息 -->
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
        <!-- <div class="tips">
          <span style="margin-right:20px;" />
          <span />
        </div> -->
      </div>
    </el-form>
    <div v-show="currentLogin == false" style="color:#fff">
      <!-- 电话号码登录 -->
      <el-form ref="mobileForm" :model="mobileForm" :rules="mobileRules" class="phoneLoginForm" auto-complete="on" label-position="left">
        <!-- 电话号码输入框 -->
        <el-form-item prop="mobile" style="margin-top:10px">
          <span class="svg-container">
            <i
              class="el-icon-mobile-phone"
              style="font-size:18px"
            />
          </span>
          <el-input
            v-model="mobileForm.mobile "
            placeholder="请输入手机号码"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <el-form-item class="elForm getCode" prop="phoneNum">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <!-- 验证码输入,点击按钮获取验证码 -->
          <el-input v-model="mobileForm.code" style="width:250px;" size="mini" placeholder="请输入手机验证码" autocomplete="off" />
          <el-button type="primary" style="margin-left:30px" :disabled="disable" :class="{ codeGeting:isGeting }" @click="getVerifyCode">{{ getCode }}</el-button>
        </el-form-item>
        <!-- 电话号码登录 忘记密码按钮 -->
        <div class="forget" @click="forget">忘记密码?</div>
        <!-- 电话号码登录 登陆提交信息 -->
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click="mobileLoginBtn">登录</el-button>
      </el-form>
    </div>

    <!-- 忘记密码表单内容 -->
    <el-dialog class="forgetPassword" width="600px" title="忘记密码" :visible.sync="dialogFormVisible">
      <el-form ref="forgetpswForm" :model="forgetpswForm" :rules="forgetpswRules" label-width="100px" class="demo-ruleForm">
        <el-form-item class="elForm" label="密码" prop="password">
          <!-- 密码输入框 -->
          <el-input v-model="forgetpswForm.password" size="mini" class="forgetPwdIpt" placeholder="请输入新的密码" type="password" show-password autocomplete="off" />
        </el-form-item>
        <el-form-item class="elForm" label="确认密码" prop="checkPass">
          <!-- 确认密码输入 -->
          <el-input v-model="forgetpswForm.checkPass" size="mini" class="forgetPwdIpt" placeholder="请再次输入密码" type="password" show-password autocomplete="off" />
        </el-form-item>
        <el-form-item class="elForm" label="手机号码" prop="mobile">
          <!-- 手机号码输入 -->
          <el-input v-model="forgetpswForm.mobile" size="mini" class="forgetPwdIpt" placeholder="请输入手机号码" autocomplete="off" />
        </el-form-item>
        <el-form-item class="elForm getCode" label="验证码">
          <!-- 验证码输入,点击按钮获取验证码 -->
          <el-input v-model="forgetpswForm.code" style="width:250px" size="mini" class="forgetPwdIpt" placeholder="请输入手机验证码" autocomplete="off" />
          <!-- <el-button style="margin-left:30px" type="primary" size="default" @click="">获取验证码</el-button> -->
          <el-button type="primary" style="margin-left:30px" :disabled="disable" :class="{ codeGeting:isGeting }" @click="forgetCode">{{ getCode }}</el-button>
        </el-form-item>
        <el-form-item class="elForm">
          <!-- 点击提交表单内容,重置密码 -->
          <el-button style="width:85%;margin-top:20px" type="primary" @click="resetPassword">重置密码</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { loginApi, getCodeNum, phoneLoginApi, forgetPasswordApi, getForgetCodeAPI, getMenuApi } from '@/api/login'
import { setToken } from '@/utils/myAuth'
import { validUsername, isvalidPass, isvalidPhone } from '@/utils/validate'
export default {
  name: 'Login',
  data() {
    // 手机号验证
    var validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入电话号码'))
      } else if (!isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    // 账号验证
    const validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号 '))
      } else if (!validUsername(value)) {
        callback(new Error('账号不存在'))
      } else {
        callback()
      }
    }

    // 密码验证
    var validatePass = function(rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入密码 '))
      } else if (!isvalidPass(value)) {
        callback(
          new Error('8到20位数字、字母或符号组成,不可为纯数字')
        )
      } else {
        callback()
      }
    }
    // 忘记密码 确认密码验证
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.forgetpswForm.password) {
        callback(new Error('两次输入的密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 验证码部分
      getCode: '获取验证码', // 验证码按钮默认文字
      isGeting: false,
      count: 60, // 倒计时60s
      disable: false, // 控制倒计时按钮 是否可用
      forgetpswForm: { // 忘记密码提交表单内容
        password: '',
        checkPass: '',
        mobile: '',
        code: ''
      },
      dialogFormVisible: false, // 忘记密码弹框
      currentLogin: true, // 判断登录方式
      mobileForm: { // 电话号码登录表单
        mobile: '', // 电话号码
        code: '' // 验证码
      },
      loginForm: { // 登录表单输入框内容
        username: 'gyy',
        password: 'gyy123456'
      },
      forgetpswRules: { // 忘记密码表单验证
        password: [{ required: true, trigger: 'blur', validator: validatePass }],
        checkPass: [{ required: true, trigger: 'blur', validator: validatePass2 }],
        mobile: [{ required: true, trigger: 'blur', validator: validPhone }]
      },
      loginRules: { // 账号密码登录校验规则
        username: [{ trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      mobileRules: { // 电话号码登录校验
        mobile: [{ required: true, trigger: 'blur', validator: validPhone }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {

  },
  methods: {
    // 重置密码
    resetPassword() {
      this.$refs.forgetpswForm.validate(valid => {
        if (valid) {
          forgetPasswordApi(this.forgetpswForm).then(res => {
            if (res.code === 200) {
              this.$message({
                message: '重置成功！',
                type: 'success'
              })
              this.dialogFormVisible = false // 关闭表单
            } else {
              this.$message({
                message: '重置失败',
                type: 'error'
              })
            }
          }
          )
        } else {
          return false
        }
      })
    },
    // 忘记密码验证请求验证码
    forgetCode() {
      this.$refs.forgetpswForm.validate(valid => {
        if (valid) {
          getForgetCodeAPI(this.forgetpswForm.mobile).then(res => {
            if (res.code === 200) {
              this.$message({
                message: '验证码已发送！',
                type: 'success' })
            } else {
              this.$message.error({
                message: `发送失败！` + res.msg
              })
            }
            // 点击按钮倒计时
            var countDown = setInterval(() => {
              if (this.count < 1) {
                this.isGeting = false
                this.disable = false
                this.getCode = '获取验证码'
                this.count = 60
                clearInterval(countDown)
              } else {
                this.isGeting = true
                this.disable = true
                this.getCode = this.count-- + 's后重发'
              }
            }, 1000)
          })
        } else {
          return false
        }
      })
    },
    // 电话号码登录
    mobileLoginBtn() {
      this.$refs.mobileForm.validate(valid => {
        if (valid) {
          this.loading = true
          phoneLoginApi(this.mobileForm.mobile, this.mobileForm.code).then(res => {
            if (res.code === 200) {
              this.$message({ message: '登录' + res.msg,
                type: 'success' })// 登陆成功
              // 存储Token
              setToken(res.data)
              // 跳转到主页
              this.$router.push({ path: '/' })
            } else {
              this.$message({
                message: '登录失败!',
                type: 'error'
              })
            }
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    // 手机号码登录请求验证码
    getVerifyCode() {
      this.$refs.mobileForm.validate(valid => {
        if (valid) {
          getCodeNum(this.mobileForm.mobile).then(res => {
            if (res.code === 200) {
              this.$message({
                message: '验证码已发送！',
                type: 'success' })
            } else {
              this.$message.error({
                message: `发送失败！` + res.msg
              })
            }
            // 点击按钮倒计时
            var countDown = setInterval(() => {
              if (this.count < 1) {
                this.isGeting = false
                this.disable = false
                this.getCode = '获取验证码'
                this.count = 60
                clearInterval(countDown)
              } else {
                this.isGeting = true
                this.disable = true
                this.getCode = this.count-- + 's后重发'
              }
            }, 1000)
          })
        } else {
          return false
        }
      })
    },
    forget() { // 忘记密码
      this.dialogFormVisible = true // 显示忘记密码弹框
    },
    phoneLogin() { // 点击手机号登录改变状态为false
      this.currentLogin = false
    },
    passwordLogin() { // 点击密码登录改变状态为true
      this.currentLogin = true
    },
    showPwd() { // 密码显示隐藏切换
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    handleLogin() { // 登录功能
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          loginApi(this.loginForm).then(res => {
            if (res.code === 200) {
              this.$message({ message: '登录' + res.msg,
                type: 'success' })// 登陆成功
              // 存储Token
              setToken(res.data)
              // 跳转到主页
              this.$router.push({ path: '/' })
            } else {
              this.$message(res.msg)
            }
            // 点击登录获取角色菜单
            getMenuApi().then(res => {
              console.log(res)
            })
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
$bg:rgba(40,86,100);
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
   input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
    .forgetPwdIpt input{
      color: #000 !important;
      caret-color: #000;
        border: 1px solid rgba(0, 0, 0, .1);
    }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
.forgetPassword .el-form-item.elForm{
  color: #000;
   background: rgba(255, 255, 255, 1);
  // border: 1px solid rgba(0, 0, 0, .1);
}
</style>

<style lang="scss" scoped>

// 改变input框背景颜色
.el-input__inner {
  background-color: transparent !important;
  border: 1px solid #1296db;
}

// 光标颜色
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
  // min-height: 100%;
  // width: 100%;
  // background-color: rgb(240, 198, 73);
  position: relative;
  overflow: hidden;
  width: 100%;
  height: 100%;
  background-image: url("../../assets/login_images/login_bg.jpg");
  background-size: cover;
  background-position: center;
  position: relative;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    margin-top: 50px;
    margin-left: 550px;
    border: rgba(0, 0, 0, 1);
    .loginType{ //登录样式
      user-select: none;
      display: flex;
      height: 30px;
      .phoneLogin{
        color: #1890ff;
        font-weight: bold;
        height: 30px;
        border-bottom: 2px solid #1890ff;
        cursor:pointer;
      }
    }
    .forget{ //忘记密码样式
      display: flex;
      font-size: 14px;
      justify-content: flex-end;
      margin-bottom: 5px;
      color: #adadad;
      user-select: none;
      cursor:pointer;
    }
  }

  .phoneLoginForm{
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 0 35px 0;
    margin: 0 auto;
    overflow: hidden;
    margin-top: 0px;
    margin-left: 550px;
    border: rgba(0, 0, 0, 1);
  }

  .loginCheck{  //选中登录样式
    user-select: none;
    color: #eee;
    height: 30px;
    font-weight: bold;
    margin-right: 20px;
     cursor:pointer;
  }

  .passwordLogin{ //选中密码登录样式
    user-select: none;
    height: 30px;
    margin-right: 20px;
    color: #1890ff;
    font-weight: bold;
    border-bottom: 2px solid #1890ff;
     cursor:pointer;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
